package Twitter
{

    
	public class ShortenURL
	{
		import flash.events.*;
		import flash.net.URLLoader;
    	import flash.net.URLLoaderDataFormat;
    	import flash.net.URLRequest;
    	import flash.net.URLVariables;	
    	import flash.net.URLRequestHeader;
    	import mx.controls.Alert;
    	import mx.managers.CursorManager;
    	import mx.utils.StringUtil;
    	
    	import mx.core.Application;
		
		public var _shorturl:String;
		
		public function ShortenURL()
		{
		}
		
		////////////////////////////
		// Shorten URL
		////////////////////////////
		
		public function shorten_url(evt:Event):void
		{
			var url:String = StringUtil.trim(Application.application.shorten_url_textbox.text)
			if (url!="")
			{
				get_shorten_url(url);
			}
		}
		
		private function get_shorten_url(url:String):void
		{
			
			var request:URLRequest = new URLRequest("http://is.gd/api.php?longurl="+url);
			
			var loader:URLLoader = new URLLoader();
			
			request.method = "GET";

			
			loader.addEventListener(Event.COMPLETE, got_url);
			loader.addEventListener(IOErrorEvent.IO_ERROR, url_ErrorHandler);
			loader.load(request);
			
			
			
			CursorManager.setBusyCursor();	
		}

		private function got_url(e:Event):void
		{

			//Alert.show(URLLoader(e.target).toString());
		
			var myHTML:URLLoader = URLLoader(e.target);
			
		
			var new_url:String = String(myHTML.data);
			
			//Alert.show(new_url);
			
			Application.application.twitter_textbox.text += " " + new_url;
			
			CursorManager.removeBusyCursor();
			
		}
		
		private function url_ErrorHandler(event:IOErrorEvent):void {
                trace("ioErrorHandler: " + event.toString());
                CursorManager.removeBusyCursor();
                
                //if (_logged_in = false)
                //{
                	Alert.show("Could not shorten_url");
                	
                //}
            }
		
	}
}